package jass.iss.edu.Properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public abstract class PropertiesDAO<PDTO extends PropertiesDTO> {
	

	private static final String DATA_FOLDER = "PropertyFiles/";
	private static final String DATE_FORMAT = "dd-MM-yyyy";
	
	private ArrayList<PDTO> dtos;
	private String pathname;


	public PropertiesDAO(String pathname) throws IOException {
		//set the path name of this property file
		this.pathname = DATA_FOLDER+pathname;
		dtos = new ArrayList<PDTO>();		
		readFile();
	}
	
	public int getTotal() {
		return dtos.size();
	}
	
	public String add(PDTO pPDTO) {
		String newID = pPDTO.getId();
		pPDTO.setId(newID);
		dtos.add(pPDTO);
		writeFile();
		return pPDTO.getId();
	}

	public void remove(String pID) {
		dtos.remove(pID);
		writeFile();
	}
	
	public PropertiesDTO get(String pID) {
		for (PDTO pdto: dtos)
			if (pdto.getId().equals(pID)) {
				return pdto;
			}
		return null;
	}
	
	public List<PDTO> getAll() {
		return new ArrayList<PDTO>(dtos);
	}
	
	public void set(PDTO pDTO) {
		for (PDTO dto: dtos) {
			if (dto.getId().equals(pDTO.getId())) {
				dtos.set(dtos.indexOf(dto), pDTO);
				writeFile();
			}
		}
	}
	
	protected abstract PDTO read(Properties pProp, String pID);
	protected abstract void write(Properties pProp, PDTO pDTO);
	
	
	public void readFile() throws IOException {
		File storeFile = new File(pathname);
		if (!storeFile.exists()) {
			System.out.println("File not found !");
			return;
		}

		Properties store = new Properties();
		
		store.load(new FileInputStream(storeFile));
		
		String idString = store.getProperty("ids");
		String[] ids = idString.split(",");
		for (String id: ids) {
			dtos.add(read(store, id));
		}
	}
	
	protected void writeFile() {
		//write DTO collection to property file
		Properties prop = new Properties();
		String ids="";
		
		//Add each DTO to property file
		for (PDTO dto: dtos) {
			write(prop, dto);
			ids=ids+dto.getId()+",";
		}
		//Add full list of id as a property in property file
		prop.setProperty("ids", ids);

		//conduct IO operation to store properties file
		try {
			FileOutputStream writer = new FileOutputStream(pathname);
			prop.store(writer, null);
		} catch (IOException ioE) {
			ioE.printStackTrace();
		}	
	}

	public Date getDateFromString(String pDate){
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT); 
		Date date = new Date();
		try {
			date = formatter.parse(pDate);
		} catch (ParseException e) {
			//e.printStackTrace();
		}
		return date;
	}
	
	public String getStringFromDate(Date pDate){
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
		return formatter.format(pDate);
	}
	


}
